package com.justalk.testapplication;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class MTextView extends AppCompatTextView {
    public MTextView(Context context) {
        super(context);
    }

    public MTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public CharSequence getText() {
        return "";
    }

    @Override
    public boolean performClick() {
        return false;
    }

    @Override
    public boolean performLongClick() {
        return false;
    }

    @Override
    public void setOnLongClickListener(@Nullable OnLongClickListener l) {
//        super.setOnLongClickListener(l);
        byte[] buff = {1,0,1,1,1};
        try {
            String result = new String(buff, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean callOnClick() {
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                return false;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        setSelectHandleDisabled();
                    }
                }, 50); // 延迟50ms，等room显示handle完后，再隐藏
                break;
            default:
                break;
        }
        return super.onTouchEvent(event);
    }

    private void setSelectHandleDisabled() {
        try {
            Field mEditor = TextView.class.getDeclaredField("mEditor");
            mEditor.setAccessible(true);
            Object object = mEditor.get(this);
            Class mClass = Class.forName("android.widget.Editor");
            // 选中时handle
            @SuppressLint("SoonBlockedPrivateApi") Method selectionController = mClass.getDeclaredMethod("getSelectionController");
            selectionController.setAccessible(true);
            Object invokeSelect = selectionController.invoke(object);
            Method hideSelect = invokeSelect.getClass().getDeclaredMethod("hide");
            hideSelect.invoke(invokeSelect);
            // 插入时handle
            @SuppressLint("SoonBlockedPrivateApi") Method insertionController = mClass.getDeclaredMethod("getInsertionController");
            insertionController.setAccessible(true);
            Object invokeInsert = insertionController.invoke(object);
            Method hideInsert = invokeInsert.getClass().getDeclaredMethod("hide");
            hideInsert.invoke(invokeInsert);
        } catch (Exception e) {

        }

    }
}
